package xyz.longicorn.driver.service;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import xyz.longicorn.driver.config.BizException;
import xyz.longicorn.driver.dao.LoginUserDao;
import xyz.longicorn.driver.dao.UserInfoMapper;
import xyz.longicorn.driver.pojo.LoginUser;
import xyz.longicorn.driver.pojo.UserInfo;

import javax.annotation.Resource;
import java.util.UUID;

@Service
public class UserService extends ServiceImpl<UserInfoMapper, UserInfo> {
    @Resource
    private LoginUserDao loginUserDao;

    public UserInfo getByAccount(String account) {
        // 查询数据条件
        QueryWrapper q = new QueryWrapper();
        // 只能根据账号查询数据
        q.eq("account", account);
        q.last("limit 1");
        return baseMapper.selectOne(q);
    }

    public UserInfo getByEmail(String email) {
        // 查询数据条件
        QueryWrapper q = new QueryWrapper();
        // 只能根据账号查询数据
        q.eq("email", email);
        q.last("limit 1");
        return baseMapper.selectOne(q);
    }

    /**
     * 使用用户名和密码登录
     *
     * @param account
     * @param password
     * @return
     */
    public LoginUser login(String account, String password) {
        UserInfo userInfo = getByAccount(account);
        //
        if (userInfo == null) throw BizException.create("用户名不正确");
        // 获取密码并验证
        String checkPassword = password + (StringUtils.hasLength(userInfo.getSalt()) ? userInfo.getSalt() : "");
        checkPassword = DigestUtils.md5DigestAsHex(checkPassword.getBytes()); // 生成用于判断的密码

        if (!checkPassword.equals(userInfo.getPassword())) throw BizException.create("登录密码不正确");
        // 将登录数据保存到redis 用户后续判断
        LoginUser user = new LoginUser();
        user.setAccount(account);
        StpUtil.login(userInfo.getId()); // 使用用户编号在sa-token 完成登录
        StpUtil.getSession().set("user", userInfo);
        //  生成接口需要的token
//        user.setToken(IdUtil.fastSimpleUUID());     // 可以使用jwt生成token
        user.setToken(StpUtil.getTokenValue());
//        loginUserDao.save(user); // 保存用户登录信息到redis
        user.setUserInfo(userInfo);
        return user;
    }

    public UserInfo signup(UserInfo userInfo) {
        // 验证数据是否正确
        if (getByAccount(userInfo.getAccount()) != null) {
            throw BizException.create("用户名已经存在了");
        }
        if (getByEmail(userInfo.getEmail()) != null) {
            throw BizException.create("邮箱已经存在了");
        }
        // 随机的加密盐
        String salt = RandomUtil.randomString(6);
        userInfo.setSalt(salt);
        // 加密密码
        userInfo.setPassword(
                DigestUtil.md5Hex(userInfo.getPassword() + salt) // 1次md5加密
        );
        this.save(userInfo);
        //

        return userInfo;
    }
}
